package com.luozy.item.web;

import com.luozy.common.dto.PageDTO;
import com.luozy.item.pojo.Item;
import com.luozy.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 1.分页查询商品
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> listPage(Integer page, Integer size) {
        log.info("查询第 {} 页商品 {} 件", page, size);
        return itemService.listPage(page, size);
    }

    //2.根据id查询商品(用于 ES数据同步(远程)、进购买界面、创建订单(远程))
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id) {
        log.info("根据id {} 查询商品", id);
        return itemService.getById(id);
    }

    //3.新增商品
    @PostMapping
    public void addItem(@RequestBody Item item) {
        log.info("新增商品");
        itemService.addItem(item);
    }

    //4.根据id修改商品状态
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("修改id {} 商品状态为 {} {}", id, status, status==1?"正常":"下架");
        itemService.updateStatus(id, status);
    }

    //5.修改商品
    @PutMapping
    public void updated(@RequestBody Item item) {
        log.info("修改商品");
        itemService.updated(item);
    }

    //6.删除商品
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        log.info("删除id {} 商品", id);
        itemService.delete(id);
    }

    //扣减库存
    @PutMapping("/stock/{itemId}/{num}")
    public void deductStock(@PathVariable Long itemId, @PathVariable Integer num) {
        log.info("扣减id {} 商品 {} 件库存", itemId, num);
        itemService.deductStock(itemId, num);
    }
}
